<?php

/**
 * 学段年级设置
 *
 * @version 1.0
 * @date 2013-12-13
 */
class DefaultController extends FController {

    public $user_school_list;
    public $user_subject_list;
    public function accessRules() {
        $config = array('index' => 1);
        return ZRole::auth("grade", $config);
    }

    public function __common() {
        parent::init();

        if (empty($this->cLoginUser)) {
            $this->redirect($this->createUrl('/site/login'));
        } else {
            $userLoginUser = $this->cLoginUser;
            $this->user_school_list = isset($userLoginUser['cuser']['school']['school_id']) ? $userLoginUser['cuser']['school']['school_id'] : '';
            $this->user_subject_list = isset($userLoginUser['cuser']['school']['subject_id']) ? $userLoginUser['cuser']['school']['subject_id'] : '';
        }

        $this->commonScript();
        Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
    }

    /**
     * @ 学段列表
     * @ yindebin
     * @ 2013-12-13 pm
     *
     */
    public function actionIndex() {
        $this->__common();
        $conn = Yii::app()->db;
        $this->title = '学段年级信息设置';
		$s['edu_code'] = Yii::app()->request->getParam('edu_code');
		$s['edu_name'] = Yii::app()->request->getParam('edu_name');

        $where = ' AND "is_del" = 0';
        $order = ' ORDER BY "edu_code" ';

		if(!empty($s['edu_code'])){
			$where.= ' AND "edu_code" LIKE \'%'.$s['edu_code'].'%\'';
		}
		if(!empty($s['edu_name'])){
			$where.= ' AND "edu_name" LIKE \'%'.$s['edu_name'].'%\'';
		}
        $select = 'SELECT * FROM "{{edu_type}}"  A';
        $sql = ' WHERE 1=1 ' . $where;

        $selectSql = $select . $sql . $order;
        $countSql = 'SELECT COUNT(*) FROM "{{edu_type}}" ' . $sql;

        $data = ZCommon::getDaoPageList($selectSql, $countSql, 8);

        $examObject = new ExamData ();
		$pageUrl = $this->createUrl ( '/school/default/index',$s );
        if (!empty($data ['listDatas'])) {
            foreach ($data ['listDatas'] as $k => $v) {
                $temp = array();
                $temp = $examObject->_getEduGrade($v ['edu_id']);

                $data ['listDatas'] [$k] ['grade_data'] = $temp;
            }
        }

		$this->render ( 'index', array ('data' => $data ['listDatas'], 'pages' => $data ['pages'], 'pageUrl' => $pageUrl, 'search'=>$s ) );
    }

    /**
     * @ 年级列表页
     * @ yindebin
     * @ 2013-12-18
     */
    public function actionGrade_list() {
        $this->__common();
        $this->title = "年级设置";
		$s['edu_id'] = (int)Yii::app()->request->getParam('edu_id');

        $where = ' AND "is_del" = 0';
        $order = ' ORDER BY "grade_code" ';

        $where .= ' AND "edu_id" = ' . "'" . $s['edu_id']. "'";

        $sql = 'SELECT * FROM "{{edu_grade}}" A';
        $condition = " WHERE 1=1 " . $where;

        $selectSql = $sql . $condition . $order;
        $countSql = 'SELECT COUNT(*) FROM "{{edu_grade}}" WHERE 1=1 ' . $where;

        $data = ZCommon::getDaoPageList($selectSql, $countSql, 8);

	$pageUrl = $this->createUrl('/school/default/grade_list',$s);

        $this->render('grade_list', array('data' => $data['listDatas'], 'pages' => $data['pages'], 'pageUrl' => $pageUrl, 'edu_id' => $s['edu_id']));
    }

    /**
     * @ 更新学段信息
     * @ yindebin pm
     * @ 2013-12-13
     *
     */
    public function actionUpdate() {
        //$this->__common ();
        $this->layout = "//layouts/main_window";

        $edu_id = (int) Yii::app()->request->getparam('edu_id');
        $edu_id = (!empty($_POST ['info'] ['edu_id'])) ? $_POST ['info'] ['edu_id'] : $edu_id;

        $model = EduType::model()->findByPk($edu_id);

        if (empty($model)) {
	    $this->frameAjaxMessage ('index', "不存在的数据" );
            exit();
        }
        if (!empty($_POST ['submit'])) {
	    $data = ZTask::dis_input($_POST ['info']);
            $examObject = new ExamData ();

            $data ['disabled'] = (!empty($data ['disabled'])) ? $data ['disabled'] : 0;
            $data['edu_code'] = strtolower($data['edu_code']);

            $check = $this->checkEduCode($data['edu_code'], $data['edu_id']);
            if (!empty($check)) {
				if($check['is_del'] == 1){
					$this->frameAjaxMessage(null,'相同代码数据在回收站种,不可重复使用');
				}else{
					$this->frameAjaxMessage(null,'代码已被占用');
				}
				
				exit;
			}
		
           	$data['update_user'] = $this->cLoginUser['cuser']['id'];
           	$data['update_time'] = time();
			$model->attributes = $data;
			if ($model->save ()) {
				$this->frameAjaxMessage ( 'index','更新成功' );
			} else {
				$this->frameAjaxMessage ( null, "更新失败" );
			}
		} else {


            $this->render('update', array('data' => $model->attributes));
        }
    }

    /**
     * @ 更新学年信息
     * @ yindebin pm
     * @ 2013-12-13
     *
     */
    public function actionUpdate_grade() {
        //$this->__common ();
        $this->layout = "//layouts/main_window";

        $grade_id = (int) Yii::app()->request->getparam('grade_id');
        $grade_id = (!empty($_POST ['info'] ['grade_id'])) ? $_POST ['info'] ['grade_id'] : $grade_id;
        $edu_id = (int) Yii::app()->request->getparam('edu_id');

        $model = EduGrade::model()->findByPk($grade_id);

        if (empty($model)) {
			$this->frameAjaxMessage ( '/school/default/grade_list/edu_id/'.$edu_id, "不存在的数据" );
            exit();
        }
        if (!empty($_POST ['submit'])) {
			$data = ZTask::dis_input($_POST['info']);
            $data ['disabled'] = (!empty($data ['disabled'])) ? $data ['disabled'] : 0;
            $data['grade_code'] = strtolower($data['grade_code']);
            $check = $this->checkGradeCode($data['grade_code'], $data['grade_id']);
            if (!empty($check)) {
				if($check['is_del'] == 1){
					$this->frameAjaxMessage(null,'相同代码数据在回收站种,不可重复使用');
				}else{
					$this->frameAjaxMessage(null,'代码已被占用');
				}
                exit;
            }

            $data['update_user'] = $this->cLoginUser['cuser']['id'];
            $data['update_time'] = time();

            $model->attributes = $data;
            if ($model->save()) {
				$this->frameAjaxMessage ( '/school/default/grade_list/edu_id/'.$data['edu_id'] );
			} else {
				$this->frameAjaxMessage ( null, "更新失败" );
            }
        } else {

            $grade_data = $model->attributes;
            $this->render('update_grade', array('grade_data' => $grade_data));
        }
    }

    /**
     * @ 添加学段
     * @ yindebin
     * @ 2013-12-13 pm
     *
     */
    public function actionAdd() {
        //$this->__common ();
        $this->layout = "//layouts/main_window";
        if (!empty($_POST ['submit'])) {
			$data = ZTask::dis_input($_POST ['info']);

            $data ['grade_id'] = 0;

            $data ['disabled'] = (!empty($data ['disabled'])) ? $data ['disabled'] : 0;
            $data['edu_code'] = strtolower($data['edu_code']);

            $check = $this->checkEduCode($data['edu_code']);
            if (!empty($check)) {
				if($check['is_del'] == 1){
					$this->frameAjaxMessage(null,'相同代码数据在回收站种,不可重复使用');
				}else{
					$this->frameAjaxMessage(null,'代码已被占用');
				}
                exit;
            }
            $data['create_user'] = $this->cLoginUser['cuser']['id'];
            $data['create_time'] = time();
            $data['update_user'] = $this->cLoginUser['cuser']['id'];
            $data['update_time'] = time();
            $model = new EduType ();
            $model->attributes = $data;

            if ($model->save()) {
				$this->frameAjaxMessage ( 'index' );
			} else {
				$this->frameAjaxMessage ( null, "添加失败" );
            }
        } else {

            $this->render('add');
        }
    }

    /**
     * @ 删除学段
     * @ yindebin
     * @ 2013-12-13 pm
     *
     */
    public function actionDelete() {
        $edu_id = (int) Yii::app()->request->getparam('edu_id');
  $conn = Yii::app()->db;

        if (!empty($edu_id)) {
            $model = EduType::model()->findByPk($edu_id);
            if (empty($model)) {
				$this->frameAjaxMessage ( 'index', "数据不存在" );
                exit();
            }
            $sql = 'UPDATE "{{edu_type}}" SET "is_del" = 1 WHERE "edu_id" = ' . $edu_id;
            $query = $conn->createCommand($sql)->execute();
            if (!empty($query)) {
                $sql = 'UPDATE "{{edu_grade}}" SET "is_del" = 1 WHERE "edu_id" = ' . $edu_id;
                $query = $conn->createCommand($sql)->execute();
				$this->frameAjaxMessage ( 'index' );
                exit();
            } else {
				$this->frameAjaxMessage ( 'index', "删除失败" );
                exit();
            }
        } else {
			$this->frameAjaxMessage ( 'index', "没有要删除的数据" );
        }
    }

    /**
     * @ 批量删除学段
     * @ yindebin
     * @ 2014-01-06 pm
     *
     */
    public function actionDelete_all() {
        $edu_id = Yii::app()->request->getparam('edu_id');
        if (!empty($edu_id)) {
			$select = 'SELECT "grade_id" FROM "{{edu_grade}}" WHERE "edu_id" IN ('.$edu_id.') AND "is_del" = 0 ';
   			$data = Yii::app()->db->createCommand($select)->queryRow();
   			if(!empty($data)){
   				echo 3;
   				exit;
   			}
			
			$delete = 'UPDATE "{{edu_type}}" SET "is_del" = 1 WHERE "edu_id" IN ('.$edu_id.')';
			$query = Yii::app()->db->createCommand($delete)->execute();
			if(!empty($query)){
				$delete = 'UPDATE "{{edu_grade}}" SET "is_del" = 1 WHERE "edu_id" IN ('.$edu_id.')';
				$query = Yii::app()->db->createCommand($delete)->execute();
				echo 1;
			}
        } else {
            echo 2;
        }
    }

    /**
     * @ 批量删除学段
     * @ yindebin
     * @ 2014-01-06 pm
     *
     */
    public function actionDelete_g_all() {
        $grade_id = Yii::app()->request->getparam('grade_id');
        if (!empty($grade_id)) {
			$delete = 'UPDATE "{{edu_grade}}" SET "is_del" = 1 WHERE "grade_id" IN ('.$grade_id.')';
            $query = Yii::app()->db->createCommand($delete)->execute();
            echo 1;
        } else {
            echo 2;
        }
    }

    /**
     * @ 添加学年
     * @ yindebin
     * @ 2013-12-13 pm
     *
     */
    public function actionAdd_grade() {
        //$this->__common ();
        $this->title = "学年设置";
        $this->layout = "//layouts/main_window";

        if (!empty($_POST ['submit'])) {
			$data = ZTask::dis_input($_POST ['info']);

            $data ['disabled'] = (!empty($data ['disabled'])) ? $data ['disabled'] : 0;
            $data['grade_code'] = strtolower($data['grade_code']);
            $check = $this->checkGradeCode($data['grade_code']);
            if (!empty($check)) {
				if($check['is_del'] == 1){
					$this->frameAjaxMessage(null,'相同代码数据在回收站种,不可重复使用');
				}else{
					$this->frameAjaxMessage(null,'代码已被占用');
				}
                exit;
            }

            $model = new EduGrade ();
            $data['create_user'] = $this->cLoginUser['cuser']['id'];
            $data['create_time'] = time();
            $data['update_user'] = $this->cLoginUser['cuser']['id'];
            $data['update_time'] = time();
            $model->attributes = $data;
			
			if ($model->save ()) { 
				$this->frameAjaxMessage ( '/school/default/grade_list/edu_id/'.$data['edu_id'] );
			} else {
				$this->frameAjaxMessage ( null, "添加失败" );
			}
        } else {
            #  取学年   #
            $edu_id = Yii::app()->request->getParam('edu_id');
            if (empty($edu_id))
				$this->frameAjaxMessage ( '/school/default/grade_list/edu_id/'.$edu_id);
			
			$edu_typ = EduType::model ()->findByPk ( $edu_id );
			if (empty ( $edu_typ ))
				$this->frameAjaxMessage ( '/school/default/grade_list/edu_id/'.$edu_id );
			
            $this->render('add_grade', array('edu_id' => $edu_id));
        }
    }

   
    /**
     * @ 删除年级
     * @ yindebin
     * @ 2013-12-13 pm
     *
     */
    public function actionDelete_grade() {
        $grade_id = (int) Yii::app()->request->getparam('grade_id');
        $edu_id = (int) Yii::app()->request->getparam('edu_id');
        if (!empty($grade_id)) {
            $model = EduGrade::model()->findByPk($grade_id);
            if (empty($model)) {
				$this->frameAjaxMessage ( '/school/default/grade_list/edu_id/'.$edu_id );
				exit ();
            }
			$delete = 'UPDATE "{{edu_grade}}" SET "is_del" = 1 WHERE "grade_id" ='.$grade_id;
			$query = Yii::app()->db->createCommand($delete)->execute();
			
			if (!empty($query)) {
				$this->frameAjaxMessage ( '/school/default/grade_list/edu_id/'.$edu_id );
				exit ();
			} else {
				$this->frameAjaxMessage ( '/school/default/grade_list/edu_id/'.$edu_id, "删除失败" );
				exit ();
			}
		} else {
			$this->frameAjaxMessage ( '/school/default/grade_list/edu_id/'.$edu_id, "没有要删除的数据" );
		}
    }

    /**
     * @ 判断学段代码是否已被占用
     * @ yindebin
     * @ 2013-12-28
     */
    protected function checkEduCode($edu_code, $edu_id = '') {
        $conn = Yii::app()->db;
        if (empty($edu_code))
            return null;

        $where = ' AND "edu_code" = ' . "'" . $edu_code . "'";
        if (!empty($edu_id)) {
            $where.= ' AND "edu_id" != ' . "'" . $edu_id . "'";
        }

        $sql = 'SELECT "edu_id","is_del"  FROM "{{edu_type}}" WHERE 1=1 ' . $where;

        return $query = $conn->createCommand($sql)->queryRow();
    }

    /**
     * @ 判断学段代码是否已被占用
     * @ yindebin
     * @ 2013-12-28
     */
    protected function checkGradeCode($grade_code, $grade_id = '') {
        $conn = Yii::app()->db;
        if (empty($grade_code))
            return null;

        $where = ' AND "grade_code" = ' . "'" . $grade_code . "'";
        if (!empty($grade_id)) {
            $where.= ' AND "grade_id" != ' . "'" . $grade_id . "'";
        }

        $sql = 'SELECT "grade_id","is_del"  FROM "{{edu_grade}}" WHERE 1=1 ' . $where;

        return $query = $conn->createCommand($sql)->queryRow();
    }

}